package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"strings"
)

func SelectDaily(ctx *gin.Context) {
	dailyType := ctx.Param("type")
	begin := ctx.Param("begin")
	end := ctx.Param("end")
	query := make([]string, 0)

	// 获取所有时间
	beginSlice := strings.Split(begin, "-")
	endSlice := strings.Split(end, "-")
	day1Int, _ := strconv.Atoi(beginSlice[2])
	day2Int, _ := strconv.Atoi(endSlice[2])
	for i := day1Int; i <= day2Int; i++ {
		dayStr := strconv.Itoa(i)
		if i < 10 {
			dayStr = "0" + strconv.Itoa(i)
		}
		d := beginSlice[0] + "-" + beginSlice[1] + "-" + dayStr
		query = append(query, d)
	}

	// 组装数据
	statisticsDailies, _ := statisticsDailyService.SelectByType(query)
	numslice := make([]int, 0)
	datalice := make([]string, 0)
	for _, v := range statisticsDailies {
		if dailyType == "register_num" {
			numslice = append(numslice, v.RegisterNum)
		} else if dailyType == "login_num" {
			numslice = append(numslice, v.LoginNum)
		} else if dailyType == "video_view_num" {
			numslice = append(numslice, v.VideoNum)
		} else if dailyType == "course_num" {
			numslice = append(numslice, v.CourseNum)
		}
		datalice = append(datalice, v.DateCalculated)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":     20000,
		"numList":  numslice,
		"dateList": datalice,
	})
	return
}
